package cn.tedu.flower.manage.content.service.impl;

import cn.tedu.flower.manage.common.ex.ServiceException;
import cn.tedu.flower.manage.common.pojo.vo.PageData;
import cn.tedu.flower.manage.common.security.CurrentPrincipal;
import cn.tedu.flower.manage.common.web.ServiceCode;
import cn.tedu.flower.manage.content.dao.persist.repository.IFadebackRepository;
import cn.tedu.flower.manage.content.pojo.entity.Fadeback;
import cn.tedu.flower.manage.content.pojo.param.FadebackAddNewParam;
import cn.tedu.flower.manage.content.pojo.param.FadebackUpdateParam;
import cn.tedu.flower.manage.content.pojo.vo.*;
import cn.tedu.flower.manage.content.service.IFadebackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class FadebackServiceImpl implements IFadebackService {
    @Value("${flower-store.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Autowired
    IFadebackRepository fadebackRepository;

    public FadebackServiceImpl() {
        log.debug("创建业务对象：FadebackServiceImpl");
    }

    @Override
    public void addNew(CurrentPrincipal currentPrincipal, String remoteAddr, FadebackAddNewParam fadebackAddNewParam) {
        log.debug("开始处理【添加反馈信息】的业务，当事人：{}，IP地址：{}，参数：{}", currentPrincipal, remoteAddr, fadebackAddNewParam);
        Fadeback fadeback = new Fadeback();
        BeanUtils.copyProperties(fadebackAddNewParam, fadeback);
        int rows = fadebackRepository.insert(fadeback);
        if (rows != 1) {
            String message = "添加反馈信息失败,服务器繁忙,请稍后再试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }


    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除会员】的业务，参数：{}", id);
        FadebackStandardVO fadebackId = fadebackRepository.selectById(id);
        if (fadebackId==null) {
            String message="删除反馈信息失败,尝试删除的反馈信息数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
        int rows = fadebackRepository.deleteById(id);
        if (rows!=1) {
            String message = "删除反馈信息失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void update(FadebackUpdateParam fadebackUpdateParam) {
        log.debug("开始处理【修改反馈信息】的业务,参数:{}",fadebackUpdateParam);
        FadebackStandardVO standardById = fadebackRepository.selectById(fadebackUpdateParam.getId());
        if (standardById==null) {
            String message="修改反馈信息失败,该反馈数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        Fadeback fadeback=new Fadeback();
        BeanUtils.copyProperties(fadebackUpdateParam,fadeback);
        int rows = fadebackRepository.update(fadeback);
        if (rows!=1) {
            String message="修改反馈信息失败,服务器繁忙,请稍后再试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }

    }


    @Override
    public FadebackStandardVO selectById(Long id) {
        FadebackStandardVO standardById = fadebackRepository.selectById(id);
        if (standardById==null) {
            String message="查询反馈信息详情失败,该反馈信息不存在";
            log.warn(message);
            throw  new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return standardById;
    }

    @Override
    public PageData<FadebackListItemVO> selectAll(Integer pageNum) {
        log.debug("开始处理【查询反馈信息列表】的业务，页码：{}", pageNum);
        PageData<FadebackListItemVO> pageData = fadebackRepository.selectAll(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<FadebackListItemVO> selectAll(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询反馈信息列表】的业务，页码：{}", pageNum);
        PageData<FadebackListItemVO> pageData = fadebackRepository.selectAll(pageNum, pageSize);
        return pageData;
    }
}
